<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #root {
        width: 100vw;
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        gap: 20px;
        flex-wrap: wrap;
      }
    </style>
    <script src="../js/vue.js" type="text/javascript"></script>
  </head>
  <body>
    <!-- 
      v-for指令：
      1.用于展示列表数据
      2.语法：v-for="(item, index) in xxx" :key="yyy"
        其中，xxx是数组，item是数组中的元素，index是索引
        yyy是每个元素的唯一标识，一般使用item中的id值
      3.可遍历：数组、对象、字符串、指定次数
     -->

    <div id="root">
      <h2>人员信息</h2>
      <button @click="addSex">动态新增性别属性</button>

      <h3>对象</h3>
      <ul>
        <li v-for="(v,k) in person" :key="k">{{ k }}-{{ v}}</li>
      </ul>

      // 列表
      <ul>
        <li v-for="p in persons" :key="p.id">
          {{ p.name }}-{{ p.age }}-{{ p.sex }}
        </li>
      </ul>
    </div>

    <script type="text/javascript">
      new Vue({
        el: "#root",
        data: {
          person: {
            id: 1,
            name: "马冬梅",
            age: 18,
          },
          persons: [
            {
              id: 1,
              name: "马冬梅",
              age: 18,
            },
            {
              id: 2,
              name: "马老师",
              age: 58,
            },
          ],
        },
        methods: {
          addSex() {
            Vue.set(this.person, "sex", "女");

            // this.$set(this.person, "sex", "女");
            this.$set(this.persons, "0", {
              ...this.persons[0],
              sex: "女",
            });
          },
        },
      });
    </script>
  </body>
</html>
